package com.itheima.ATM;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> Accouns = new ArrayList<>(); //创建一个账户的集合
    private static Scanner sn = new Scanner(System.in);
    private Account loginass;

    private void cardInfo() {
        Account card = new Account();
        //录入开户信息
        System.out.println("======欢迎来到新卡开户界面======");
        System.out.println("请输入您的名字：");
        String name = sn.next();
        card.setUsername(name);
        while (true) {
            System.out.println("请输入您的性别：");
            char sex = sn.next().charAt(0);
            if (sex == '男' || sex == '女') {
                card.setSex(sex);
                break;
            } else {
                System.out.println("对不起，您输入的性别有误~");
            }
        }

        while (true) {
            System.out.println("请您设置该卡的密码：");
            String password1 = sn.next();
            System.out.println("请您再次输入该卡的密码：");
            String password2 = sn.next();
            if (!password2.equals(password1)) {
                System.out.println("您输入的两次密码不一致~");
            } else {
                System.out.println("密码设置成功~");
                card.setPassWord(password1);
                break;
            }

        }

        while (true) {
            System.out.println("请您输入该卡每次提现的额度：");
            double limit = sn.nextDouble();
            if (limit > 0 && limit <= 10000) {
                System.out.println("提现额度设置成功,您每次最多可提现" + limit + "￥~");
                card.setLimit(limit);
                break;
            } else {
                System.out.println("请输入1 - 10000范围内的金额~");
            }
        }

        //生成随机卡号，并且不能重复
        String scardid = randomCardid(8);
        card.setCardId(scardid);

        //存储开卡信息
        Accouns.add(card);

        //开卡成功信息提醒
        System.out.println("恭喜您" + card.getUsername() + "开户成功,您的卡号是：" + card.getCardId());
    }

    //生成卡号
    private String randomCardid(int number) {
        Random ra = new Random();
        String text = "";
        while (true) {
            for (int i = 0; i < number; i++) {
                text += ra.nextInt(10);
            }
            //判断该卡号是否与其他卡号是否重复
            Account cardid = getAccountByCardId(text);
            if (cardid == null) {
                return text;
            }
        }
    }

    //查询卡号
    private Account getAccountByCardId(String cardid) {
        for (int i = 0; i < Accouns.size(); i++) {
            Account usercard = Accouns.get(i);
            if (usercard.getCardId().equals(cardid)) {
                return usercard;
            }
        }
        return null;
    }

    //登录界面
    private void logain() {
        System.out.println("======欢迎来到卡号登录界面======");
        if (Accouns.size() == 0) {
            System.out.println("请您先开户再来登录~");
            return;
        }

        while (true) {
            System.out.println("请您输入登录卡号：");
            String cardid = sn.next();
            //判断该卡号是否与其他卡号是否重复
            Account cardId = getAccountByCardId(cardid);
            if (cardId == null) {
                System.out.println("您输入的卡号不存在~");
            } else {
                while (true) {
                    System.out.println("请您输入卡号密码：");
                    String password = sn.next();
                    //判断密码是否正确
                    if (cardId.getPassWord().equals(password)) {
                        //密码正确
                        System.out.println("恭喜您," + cardId.getUsername() + "成功登录系统,您的卡号是：" + cardId.getCardId());
                        //登录以后的功能
                        loginass = cardId;
                        loginprint();
                        return;
                    } else {
                        System.out.println("您输入的密码不正确，请确认是否继续登录(y/n)：");
                        String command = sn.next();
                        if (Objects.equals(command, "n")) {
                            return;
                        }
                    }
                }
            }
        }
    }

    //登录后的功能
    private void loginprint() {
        while (true) {
            System.out.println("====" + loginass.getUsername() + ",您可以办理以下业务====");
            System.out.println("(1)-----查询账户-----");
            System.out.println("(2)-------存款------");
            System.out.println("(3)-------取款------");
            System.out.println("(4)-------转账------");
            System.out.println("(5)-----修改密码-----");
            System.out.println("(6)-----注销卡号-----");
            System.out.println("(7)-----取卡退出-----");
            System.out.println("请选择：");
            String command = sn.next();
            switch (command) {
                case "1"://账户信息
                    showloginAccount();
                    break;
                case "2"://存款
                    saveMoney();
                    break;
                case "3"://取款
                    darwMoney();
                    break;
                case "4"://转账
                    transferMoney();
                    break;
                case "5"://修改密码
                    newPassWord();
                    return;
                case "6"://注销卡号
                    // 注销成功回到ATM界面
                    if (deletecard()) {
                        return;
                    }
                    // 注销失败回到功能界面
                    break;
                case "7"://退卡
                    System.out.println(loginass.getUsername() + "您取卡退出成功~");
                    return;
                default:
                    System.out.println("对不起，您输入的命令有误~");
            }
        }
    }

    //查询账户信息
    private void showloginAccount() {
        System.out.println("=======账户信息如下=======");
        System.out.println("卡号：" + loginass.getCardId());
        System.out.println("卡主：" + loginass.getUsername());
        System.out.println("性别：" + loginass.getSex());
        System.out.println("余额：" + loginass.getMoney() + "￥");
        System.out.println("每次提现额度：" + loginass.getLimit() + "￥");
    }

    //转账
    private void transferMoney() {
        System.out.println("=======用户转账=======");
        if (Accouns.size() < 2) {
            System.out.println("当前系统账户不足两个,不允许转账~");
            return;
        }
        if (loginass.getMoney() == 0) {
            System.out.println("当前账户余额为0￥,不允许转账~");
            return;
        }
        while (true) {
            System.out.println("请您输入转账卡号：");
            String cardid = sn.next();
            //判断该卡号是否与其他卡号是否重复
            Account cardId = getAccountByCardId(cardid);
            if (cardId == null) {
                System.out.println("对不起，您输入的卡号不存在~");
            } else {
                String name = cardId.getUsername().substring(1);
                System.out.println("请您输入【*" + name + "】的姓氏：");
                String surname = sn.next();
                //判断输入的姓氏
                if (cardId.getUsername().startsWith(surname)) {
                    //开始转账
                    while (true) {
                        System.out.println("请您输入转账金额：");
                        double money = sn.nextDouble();
                        if (loginass.getMoney() >= money) {
                            //转给对方
                            cardId.setMoney(cardId.getMoney() + money);
                            //扣掉转账金额
                            loginass.setMoney(loginass.getMoney() - money);
                            System.out.println("恭喜您,您向【" + cardId.getUsername() + "】转账" + money + "￥成功,转账后的余额是：" + loginass.getMoney()+"￥");
                            return;
                        } else {
                            System.out.println("您的账户余额不足,您的余额是：" + loginass.getMoney());
                        }
                    }
                } else {
                    System.out.println("对不起，您输入的姓氏有问题~");
                }
            }
        }
    }

    //注销操作
    private boolean deletecard() {
        System.out.println("=======卡号注销=======");
        System.out.println("请您确认注销该卡号吗？(y/n)");
        String command = sn.next();
        if (Objects.equals(command, "y")) {
            if (loginass.getMoney() == 0) {
                Accouns.remove(loginass);
                System.out.println("您好，您的卡号注销成功~");
                return true;
            } else {
                System.out.println("对不起，您的卡号存有金额，不允许注销~");
                return false;
            }
        } else {
            System.out.println("好的，取消注销操作,卡号保留~");
            return false;
        }
    }

    //修改密码
    private void newPassWord() {
        System.out.println("=======密码修改=======");
        while (true) {
            System.out.println("请您输入【当前】卡号密码：");
            String password = sn.next();
            if (loginass.getPassWord().equals(password)) {
                while (true) {
                    //第一次输入密码
                    System.out.println("请您输入卡号新密码：");
                    String newpassword1 = sn.next();
                    //再次确认密码
                    System.out.println("请您再次输入新密码：");
                    String newpassword2 = sn.next();
                    if (!newpassword2.equals(newpassword1)) {
                        System.out.println("您输入的两次密码不一致~");
                    } else {
                        //修改密码
                        if (loginass.getPassWord().equals(newpassword2)) {
                            System.out.println("对不起，设置的新密码必须与原密码不同~");
                        } else {
                            System.out.println("恭喜您," + loginass.getUsername() + ",该卡密码修改成功~");
                            loginass.setPassWord(newpassword2);
                            System.out.println("请重新进行登录~");
                            return;
                        }
                    }
                }

            } else {
                //判断密码是否正确
                System.out.println("您输入的密码不正确，请确认是否继续修改(y/n)：");
                String command = sn.next();
                if (Objects.equals(command, "n")) {
                    return;
                }
            }
        }
    }

    //存钱
    private void saveMoney() {
        System.out.println("=======存款=======");
        System.out.println("请您输入存款金额：");
        double savemoney = sn.nextDouble();
        //更新存款
        loginass.setMoney(loginass.getMoney() + savemoney);
        System.out.println("恭喜您,您存入" + savemoney + "￥成功,存款后的余额是：" + loginass.getMoney()+"￥");
    }

    //取钱
    private void darwMoney() {
        System.out.println("=======取款=======");
        if (loginass.getMoney() < 100) {
            System.out.println("您的账户余额不足100￥,不允许取款~");
            return;
        }

        while (true) {
            System.out.println("请您输入取款金额：");
            double darwmoney = sn.nextDouble();
            //更新存款
            if (loginass.getMoney() >= darwmoney) {
                //余额足够
                if (darwmoney > loginass.getLimit()) {
                    System.out.println("当前取款金额超过每次提现额度,您每次最多可取：" + loginass.getLimit() + "￥");
                } else {
                    loginass.setMoney(loginass.getMoney() - darwmoney);
                    System.out.println("恭喜您,您取出" + darwmoney + "￥成功,取款后的余额是：" + loginass.getMoney() + "￥");
                    break;
                }
            } else {
                System.out.println("您的账户余额不足,您的余额是：" + loginass.getMoney() + "￥");
            }
        }
    }


    /**
     * 启动ATM系统界面
     */
    public void start() {
        while (true) {
            System.out.println("======欢迎来到ATM系统======");
            System.out.println("(login)-----卡号登录-----");
            System.out.println("(register)--新卡开户-----");
            System.out.println("(quit)------退出系统-----");
            System.out.println("=========================");
            System.out.println("请您选择要操作的命令：");
            String command = sn.next();
            switch (command) {
                case "login":
                    logain();
                    break;
                case "register":
                    cardInfo();
                    break;
                case "quit":
                    System.out.println("已为您退出ATM系统~");
                    return;
                default:
                    System.out.println("对不起，您输入的命令有误~");
            }
        }
    }
}
